package yanyan.com.tochar;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.gs.buluo.common.widget.LoadingDialog;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import yanyan.com.tochar.utils.BitmapPixelUtil;
import yanyan.com.tochar.utils.CoreUtil;
import yanyan.com.tochar.utils.PictureSelectUtil;
import yanyan.com.tochar.utils.ToastUtil;

/**
 *黑白图片生成
 */
public class FunsBlackAndWhiteImgActivity extends Activity {
    private ImageView resImg,inImg;
    private Bitmap newBitmap,oldBitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_funs_black_white_img);
        resImg=findViewById(R.id.black_white_res);
        inImg=findViewById(R.id.black_white_img);

        //统计各功能
        CoreUtil.statisticalFuns(this,"funs_black_white");
    }

    public void getBlackWhiteImg(View view){
        if(oldBitmap==null){
            ToastUtil.showLongToast(this,"请选择一张图片");
            return;
        }
        LoadingDialog.getInstance().show(FunsBlackAndWhiteImgActivity.this, "正在生成", false);//加载
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                try {
                    Bitmap bitmap = BitmapPixelUtil.getBlackAndWhiteImg(oldBitmap);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            newBitmap=bitmap;
                            resImg.setImageBitmap(bitmap);
                        }
                    });
                    e.onComplete();
                } catch (Exception e1) {
                    e.onError(e1);
                }

            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(String s) {

            }

            @Override
            public void onError(Throwable e) {
                LoadingDialog.getInstance().dismissDialog();
                ToastUtil.showShotToast(FunsBlackAndWhiteImgActivity.this, "生成失败");
            }

            @Override
            public void onComplete() {
                LoadingDialog.getInstance().dismissDialog();
                ToastUtil.showShotToast(FunsBlackAndWhiteImgActivity.this, "生成成功");
            }
        });


    }


    /**
     * 选择图片
     * @param view
     */
    public void selectBlackWhiteImg(View view){
        PictureSelectUtil.selectPohto(this,3306);
    }


    public void savenBlackWhiteImg(View view){
        if(newBitmap!=null){
            String s = PictureSelectUtil.savePhotoToSD(newBitmap, this);
            ToastUtil.showShotToast(this,"图片已保存至"+s);
        }else {
            ToastUtil.showShotToast(this,"您还没有生成");
        }

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK){
            List<LocalMedia> localMedias = PictureSelector.obtainMultipleResult(data);
            String path="";
            if(localMedias!=null && localMedias.size()>0){
                LocalMedia localMedia = localMedias.get(0);
                if(CoreUtil.isAndroidQ()){
                    path=localMedia.getAndroidQToPath();
                }else {
                    path= localMedia.getPath();
                }
                    oldBitmap = PictureSelectUtil.getBitmap(path);
                inImg.setImageBitmap(oldBitmap);
            }
        }else {
            ToastUtil.showShotToast(this,"图片选择失败");
        }
    }

}
